package org.theonept.travian.model;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.theonept.travian.util.HttpUtil;

import android.text.format.DateUtils;

public class Rapport implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8668893524734090707L;
	private TypeRapport typeRapport;
	private String sujet;
	private Date date;
	private AbstractRapportContent rapportContent;
	private Compte compte;
	private String id;
	private Boolean read;
	private EnumReward reward;

	/**
	 * @return the typeRapport
	 */
	public TypeRapport getTypeRapport() {
		return typeRapport;
	}

	/**
	 * @param typeRapport
	 *            the typeRapport to set
	 */
	public void setTypeRapport(TypeRapport typeRapport) {
		this.typeRapport = typeRapport;
	}

	/**
	 * @return the date
	 */
	public Date getDate() {
		return date;
	}

	/**
	 * @param date
	 *            the date to set
	 */
	public void setDate(Date date) {
		this.date = date;
	}

	/**
	 * @return the rapportContent
	 */
	public AbstractRapportContent getRapportContent() {
		return rapportContent;
	}

	/**
	 * @param rapportContent
	 *            the rapportContent to set
	 */
	public void setRapportContent(AbstractRapportContent rapportContent) {
		this.rapportContent = rapportContent;
	}

	public static List<Rapport> getRapports(Compte compte) {
		// TODO Auto-generated method stub http://ts5.travian.fr/berichte.php
		SimpleDateFormat sm = new SimpleDateFormat("dd.MM.yy hh:mm");
		SimpleDateFormat sm2 = new SimpleDateFormat("dd.MM.yy");
		List<Rapport> rapports = new ArrayList<Rapport>();
		Element rapportsElements = HttpUtil.getDocumentFromUrl(compte.getUrlServeur() + "berichte.php").getElementById(
				"content");
		Elements lastPage = rapportsElements.getElementsByAttributeValue("class", "last");
		Integer nbpage = 1;
		if (!lastPage.isEmpty()) {
			String nbPageTemp = lastPage.first().attr("href").split("page=")[1];
			nbpage = Integer.valueOf(nbPageTemp);
		}

		for (int i = 1; i <= nbpage; i++) {
			Document documentPage = HttpUtil.getDocumentFromUrl(compte.getUrlServeur() + "berichte.php?&o=0&page=" + i);
			Element content = documentPage.getElementById("overview");
			List<Element> listRapports = content.children().last().children();
			for (Element element : listRapports) {

				if (!element.getElementsByAttributeValueStarting("href", "berichte.php?id").isEmpty()) {
					Rapport rapport = new Rapport();

					Element first = element.getElementsByAttributeValueStarting("href", "berichte.php?id").first();
					rapport.setSujet(first.text().trim());
					rapport.setId(first.attr("href").replace("berichte.php?id=", "").replace("&t=", ""));
					String dateText = element.getElementsByAttributeValue("class", "dat").first().text();
					String codeType = element.getElementsByAttributeValueStarting("class", "iReport").first()
							.attr("class").split(" ")[1];
					Elements elementsreportInfo = element.getElementsByAttributeValueStarting("class", "reportInfo");
					if (!elementsreportInfo.isEmpty()) {
						String attr = elementsreportInfo.first().children().first().attr("class");
						List<String> reportInfo = Arrays.asList(attr.split(" "));

						rapport.setReward(EnumReward.getRewardFromCode(reportInfo.get(reportInfo.size() - 1)));
					}
					rapport.setTypeRapport(TypeRapport.getTypeFromCode(codeType));
					try {

						rapport.setDate(sm.parse(dateText));
					} catch (ParseException e) {
						String heure = dateText.split(" ")[1];
						Date time = new Date(System.currentTimeMillis() - DateUtils.DAY_IN_MILLIS);

						try {
							rapport.setDate(sm.parse(sm2.format(time) + " " + heure));
						} catch (ParseException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					}
					rapport.setRead(!element.getElementsByAttributeValueStarting("class", "messageStatus").first()
							.attr("class").contains("messageStatusUnread"));
					rapport.setCompte(compte);
					rapports.add(rapport);
				}
			}
		}

		return rapports;
	}

	/**
	 * @return the compte
	 */
	public Compte getCompte() {
		return compte;
	}

	/**
	 * @param compte
	 *            the compte to set
	 */
	public void setCompte(Compte compte) {
		this.compte = compte;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the sujet
	 */
	public String getSujet() {
		return sujet;
	}

	/**
	 * @param sujet
	 *            the sujet to set
	 */
	public void setSujet(String sujet) {
		this.sujet = sujet;
	}

	/**
	 * @return the read
	 */
	public Boolean isRead() {
		return read;
	}

	/**
	 * @param read
	 *            the read to set
	 */
	public void setRead(Boolean read) {
		this.read = read;
	}

	/**
	 * @return the reward
	 */
	public EnumReward getReward() {
		return reward;
	}

	/**
	 * @param reward
	 *            the reward to set
	 */
	public void setReward(EnumReward reward) {
		this.reward = reward;
	}

}
